package com.lagou.ipersistence.reflection;

import javax.xml.crypto.Data;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author Yan Jiahong
 * Created on 2020/11/17
 */
public class DefaultReflectorFactory implements ReflectorFactory {
    private boolean cacheEnabled = true;
    
    private final ConcurrentMap<Class<?>, Reflector> reflectorMap = new ConcurrentHashMap<>();
    
    public DefaultReflectorFactory() {
    }
    
    @Override
    public boolean isClassCacheEnabled() {
        return cacheEnabled;
    }
    
    @Override
    public void setClassCacheEnabled(boolean cacheEnabled) {
        this.cacheEnabled = cacheEnabled;
    }
    
    @Override
    public Reflector findClass(Class<?> type) {
        if (cacheEnabled) {
            return reflectorMap.computeIfAbsent(type, Reflector::new);
        } else {
            return new Reflector(type);
        }
    }
}
